package cn.com.nes.site.listener;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.module.service.crm.OpportunityService;


//@Service
public class BimTimerUtil implements ApplicationListener<ContextRefreshedEvent> {
	
	@Autowired
	private OpportunityService service;
	
    @Override
    public void onApplicationEvent(ContextRefreshedEvent evt) {
	    if (evt.getApplicationContext().getParent() == null) {
	        createTimerTask();
	    }
     }
	
    private void createTimerTask() {
        Timer timer = new Timer("createTimerTask", true);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
            	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            	try{
            		String startTime = sdf.format(new Date());
                	//更新设计列表
                	System.out.println("-[START BIMLIST] : " + startTime + "-");
                	service.updateBimList();
                	String bimTime = sdf.format(new Date());
                	System.out.println("-[END BIMLIST] : " + bimTime + " [PASS BY] : " + (sdf.parse(bimTime).getTime()-sdf.parse(startTime).getTime()));
                	//更新清单列表
                	System.out.println("-[START DESIGNLIST] : " + bimTime + "-");
                	DataReq req = new DataReq();
                	service.updateDesignDetail(req);
                	String designTime = sdf.format(new Date());
                	System.out.println("-[END DESIGNIST] : " + designTime + " [PASS BY] : " + (sdf.parse(designTime).getTime()-sdf.parse(bimTime).getTime()));
            	}catch(Exception e){
            		e.printStackTrace();
            	}
            	
            	
            }
        }, 1000*10*1,1000*7000*1);
    }
    
    
    public static void main(String args[]){
    	Timer timer = new Timer();
    }
}
